package com.lzy.product.apis.impl;

import com.lzy.framework.tools.commons.BigDecimalUtil;
import com.lzy.product.apis.CouponProfitRecordServiceApi;
import com.lzy.product.apis.MemberCouponsServiceApi;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.CouponProfitRecordDto;
import com.lzy.product.dtos.MemberCouponsDto;
import com.lzy.product.service.MemberCouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 会员卡券宝详情服务
 * Created by WIN7 on 2017/12/29.
 */
@Service
public class MemberCouponsServiceApiImpl implements MemberCouponsServiceApi {

    @Autowired
    private MemberCouponsService memberCouponsService;

    @Autowired
    private CouponProfitRecordServiceApi couponProfitRecordServiceApi;

    @Override
    public Long insert(MemberCouponsDto dto) {
        return memberCouponsService.insert(dto);
    }

    @Override
    public Boolean update(MemberCouponsDto dto) {
        return memberCouponsService.update(dto);
    }

    @Override
    public Boolean delete(Long id) {
        return null;
    }

    @Override
    public Boolean remove(Long id) {
        return null;
    }

    @Override
    public MemberCouponsDto findById(Long id) {
        return memberCouponsService.findById(id);
    }

    @Override
    public List<MemberCouponsDto> query(MemberCouponsDto dto) {
        return memberCouponsService.query(dto);
    }

    @Override
    public Boolean updateMemberCoupons(MemberCouponsDto paramDto) {
        if (paramDto.getMemberId() != null) {
            MemberCouponsDto dto = memberCouponsService.findByMemberIdAndType(paramDto.getMemberId(), paramDto.getType()!=null ? paramDto.getType() : BusinessType.MemberCouponsType.PROFIT.getCode());
            if (dto != null) {
                if(paramDto.getLimitedMoney() == null
                        && paramDto.getAvailableMoney() == null
                        && paramDto.getDragonMoney() == null) {
                    return true;
                }
                MemberCouponsDto updateDto = new MemberCouponsDto();
                updateDto.setId(dto.getId());
                updateDto.setType(paramDto.getType());
                if (paramDto.getLimitedMoney() != null) {
                    //限制金额
                    updateDto.setLimitedMoney(BigDecimalUtil.add(paramDto.getLimitedMoney(), dto.getLimitedMoney()));

                    //限制金额为正的时候，表示有额外收益，比如：佣金，添加一条收益记录
                    if (paramDto.getLimitedMoney().compareTo(new BigDecimal(0)) > 0 && BusinessType.MemberCouponsType.COMMISSION.getCode().equals(paramDto.getType())){
                        CouponProfitRecordDto couponProfitRecordDto = new CouponProfitRecordDto();
                        couponProfitRecordDto.setMemberId(paramDto.getMemberId());
                        couponProfitRecordDto.setAmount(paramDto.getLimitedMoney());
                        couponProfitRecordDto.setOrderId(paramDto.getOrderId());
                        couponProfitRecordDto.setStatus(BusinessType.CouponProfitStatus.LIMITED.getCode());
                        couponProfitRecordDto.setType(2);
                        couponProfitRecordDto.setLimitDay(30);
                        if (couponProfitRecordServiceApi.insert(couponProfitRecordDto) <= 0){
                            return Boolean.FALSE;
                        }
                    }
                }
                if (paramDto.getAvailableMoney() != null) {
                    //可提现金额
                    updateDto.setAvailableMoney(BigDecimalUtil.add(paramDto.getAvailableMoney(), dto.getAvailableMoney()));
                }
                if (paramDto.getDragonMoney() != null) {
                    //龙币金额
                    updateDto.setDragonMoney(BigDecimalUtil.add(paramDto.getDragonMoney(), dto.getDragonMoney()));
                }
                return memberCouponsService.update(updateDto);
            } else {
                Long res = memberCouponsService.insert(paramDto);
                return res > 0;
            }
        }
        return Boolean.FALSE;
    }

    @Override
    public MemberCouponsDto findByMemberId(Long memberId) {
        return memberCouponsService.findByMemberId(memberId);
    }

    @Override
    public MemberCouponsDto findByMemberIdAndType(Long memberId, Integer type) {
        return memberCouponsService.findByMemberIdAndType(memberId, type);
    }

}
